<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class CI_Tournament_entity{
	var $id = 0;
	var $sport_id = 0;
	var $name = "";
	var $start_time = 0;
	var $end_time = 0;
	var $status = "";
	var $description = "";
	var $created_date = 0;
	var $entry_fee = 0;
	var $free_coins = 10;
	var $publish = "";
	var $is_finish = 'no'; 
	var $number_top_rank = 1000;
	var $limit_coin = -1;
	var $min_coin = 5;
	var $show_tournament_rank = 1;
	var $show_weekly_rank = 1;
	
	function __destruct() {}
	function __construct() {}
    
	function init($cmd, $input)
	{
		$ci=& get_instance();
		
		if($cmd == 'id')
		{
			$result = $ci->db->where('id',$input)->get(TBL_TOURNAMENT)->result();
		}
		else
		{
			return false;
		}
		
		if( !empty($result) )
		{
			$this->id = $result[0]->id;
			$this->name = $result[0]->name;
			$this->sport_id = $result[0]->sport_id;
			$this->start_time = $result[0]->start_time;
			$this->end_time = $result[0]->end_time;
			$this->status = $result[0]->status;
			$this->description = $result[0]->description;
			$this->created_date = $result[0]->created_date;
			$this->entry_fee = $result[0]->entry_fee;
			$this->free_coins = $result[0]->free_coins;
			$this->publish = $result[0]->publish;
			$this->is_finish = $result[0]->is_finish;
			$this->number_top_rank = $result[0]->number_top_rank;
			$this->limit_coin = $result[0]->limit_coin;
			$this->min_coin = $result[0]->min_coin;
			$this->show_tournament_rank = $result[0]->show_tournament_rank;
			$this->show_weekly_rank = $result[0]->show_weekly_rank;
			
			return $this;
		}
		return false;
	}
	
	function setName($input){ $this->name=$input; }
	function setSportId($input){ $this->sport_id=$input;}
	function setStartTime($input){ $this->start_time=$input; }
	function setEndTime($input){ $this->end_time=$input; }
	function setStatus($input){ $this->status=$input; }
	function setDescription($input){ $this->description=$input; }
	function setCreatedDate($input){ $this->created_date=$input; }
	function setEntryFee($input){ $this->entry_fee=$input; }
	function setFreeCoins($input){ $this->free_coins=$input; }
	function setPublish($input){ $this->publish=$input; }
	function setIsFinish($input){ $this->is_finish=$input; }
	function setNumberTopRank($input){ $this->number_top_rank=$input; }
	function setLimitCoin($input){ $this->limit_coin=$input; }
	function setMinCoin($input){ $this->min_coin=$input; }
	function setShowTournamentRank($input){ $this->show_tournament_rank=$input;}
	function setShowWeeklyRank($input){ $this->show_weekly_rank=$input;}
	
	function getId(){ return $this->id; }
	function getSportId(){ return $this->sport_id; }
	function getName(){ return $this->name; }
	function getStartTime(){ return $this->start_time; }
	function getEndTime(){ return $this->end_time; }
	function getStatus(){ return $this->status; }
	function getDescription(){ return $this->description; }
	function getCreatedDate(){ return $this->created_date; } 
	function getEntryFee(){ return $this->entry_fee; }
	function getFreeCoins(){ return $this->free_coins; }
	function getPublish(){ return $this->publish; }
	function getIsFinish(){ return $this->is_finish; }
	function getNumberTopRank(){ return $this->number_top_rank; }
	function getLimitCoin(){ return $this->limit_coin; }
	function getMinCoin(){ return $this->min_coin; }
	function getShowTournamentRank(){ return $this->show_tournament_rank;}
	function getShowWeeklyRank(){ return $this->show_weekly_rank;}
	
	function update()
	{
		$ci=& get_instance();
		 
		$ci->db->set('name', $this->name);
		$ci->db->set('start_time', $this->start_time);
		$ci->db->set('end_time', $this->end_time);
		$ci->db->set('sport_id', $this->sport_id);
		$ci->db->set('status', $this->status);
		$ci->db->set('description', $this->description);
		$ci->db->set('created_date', $this->created_date);
		$ci->db->set('entry_fee', $this->entry_fee);
		$ci->db->set('free_coins', $this->free_coins);
		$ci->db->set('publish', $this->publish);
		$ci->db->set('is_finish', $this->is_finish);
		$ci->db->set('number_top_rank', $this->number_top_rank);
		$ci->db->set('limit_coin', $this->limit_coin);
		$ci->db->set('min_coin', $this->min_coin);
		$ci->db->set('show_tournament_rank', $this->show_tournament_rank);
		$ci->db->set('show_weekly_rank', $this->show_weekly_rank);
		
		$ci->db->where('id', $this->id)->update(TBL_TOURNAMENT);
	}
	
	function createNewTournamentEntity($name){
		$ci=& get_instance();
		
		$ci->db->set('name',$name);
		$ci->db->insert(TBL_TOURNAMENT);
		
		return $ci->db->insert_id();
	}
	
	function getArrayListOfTournament(){
		$ci=& get_instance();
		return $ci->db->get(TBL_TOURNAMENT)->result();
	}
	
	function getTournamentByStatus($status= "living"){
		$ci=& get_instance();
		if($status == "living"){
			return $ci->db->where("(status LIKE 'living' OR status LIKE 'closed') AND publish LIKE 'publish' AND is_finish LIKE 'no' ")
					->get(TBL_TOURNAMENT)->result();
		}	
		if($status == "upcoming"){
			return $ci->db->where("status LIKE 'upcoming' AND publish LIKE 'publish' AND is_finish LIKE 'no' ")
					->get(TBL_TOURNAMENT)->result();
		}
		if($status == "closed"){
			return $ci->db->where("status LIKE 'closed' AND publish LIKE 'publish' AND is_finish LIKE 'yes' ")
					->get(TBL_TOURNAMENT)->result();
		}
		if($status == "cancelled"){
			return $ci->db->where("status LIKE 'cancelled' AND publish LIKE 'publish'")
					->get(TBL_TOURNAMENT)->result();
		}
	}
	
	function getUnpublishTournament(){
		$ci=& get_instance();
		return $ci->db->where("publish LIKE 'unpublish'")->get(TBL_TOURNAMENT)->result();
	}
	
	function changeStatusTournamentRealTime(){
		$ci=& get_instance();
		$ci->db->where("status LIKE 'upcoming' AND start_time <=".time())->update(TBL_TOURNAMENT, array('status'=>'living'));
		$ci->db->where("status LIKE 'living' AND end_time <=".time())->update(TBL_TOURNAMENT, array('status'=>'closed'));		
	}
	
}
